package com.example.dao.impl;

import com.example.dao.UserDao;
import com.example.model.Admin;
import com.example.model.User;
import com.example.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {


    @Override
    public List<User> getUsersByCondition(Map<String,String> params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Admin admin = null;
        String username = params.get("username");
        String state = params.get("state");
        int currentPage = Integer.parseInt(params.get("currentPage"));
        int pageSize = Integer.parseInt(params.get("pageSize"));
        int dataOff = (currentPage -1) * pageSize;
        List<User> userList = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select id,username,nickname,money,state from tb_user where 1=1 ";
            if(username != null && !username.equals("")) {
                sql += " and nickname = '"+username + "'";
            }
            if(state != null && !state.equals("")) {
                int _state = Integer.parseInt(state);
                sql += " and state = "+ _state;
            }
            sql += " limit ?,?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,dataOff);
            ps.setInt(2,pageSize);
            rs = ps.executeQuery();
            while (rs.next()){
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setNickName(rs.getString("nickname"));
                user.setMoney(rs.getDouble("money"));
                user.setState(rs.getInt("state"));
                userList.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return userList;
    }

    @Override
    public int getTotal(Map<String, String> params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int total = 0;
        String username = params.get("username");
        String state = params.get("state");
        try {
            conn = DBUtil.getConnection();
            String sql = "select count(*) from tb_user where 1=1 ";
            if(username != null && !username.equals("")) {
                sql += " and nickname = '"+username +"'";
            }
            if(state != null && !state.equals("")) {
                int _state = Integer.parseInt(state);
                sql += " and state = "+ _state;
            }
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                total = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return total;
    }

}
